package com.mida.app.knw.controller;

import com.mida.app.knw.service.KnwNoumenonRuleService;
import com.mida.app.knw.service.KnwNoumenonService;
import com.mida.dms.knw.entity.KnwNoumenonUserSpace;
import com.mida.dms.knw.vo.*;
import com.mida.tool.common.result.ActionResult;
import com.mida.tool.common.result.ListResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.mida.tool.common.result.BaseResult.buildActionWarn;
import static com.mida.tool.common.result.BaseResult.buildListWarn;

/**
 * <h1>本体前台控制器</h1>
 * <p>
 *
 * </p>
 *
 * @author jian.li on 2021/6/21 17:08
 */
@Api(tags = "本体")
@RestController
@RequestMapping("/knwNoumenon")
@CrossOrigin
@Slf4j
public class KnwNoumenonController {

    @Autowired
    KnwNoumenonService knwNoumenonService;

    @Autowired
    KnwNoumenonRuleService knwNoumenonRuleService;

    /**
     * 新增本体(草稿状态,含规则)
     * @param knwNoumenonVo
     * @return
     */
    @ApiOperation(value = "新增本体",notes = "新增本体")
    @PostMapping(value = "/addKnwNoumenon")
    @ResponseBody
    public ActionResult add(@RequestBody KnwNoumenonVo knwNoumenonVo){
        return knwNoumenonService.add(knwNoumenonVo);
    }

    /**
     * 更新本体(含规则)
     * @param knwNoumenonVo
     * @return
     */
    @ApiOperation(value = "更新本体",notes = "更新本体")
    @PostMapping(value = "/updateKnwNoumenon",consumes = {"application/json;charset=UTF-8"})
    @ResponseBody
    public ActionResult update(@RequestBody KnwNoumenonVo knwNoumenonVo){
        return knwNoumenonService.update(knwNoumenonVo);
    }

    /**
     *删除本体(含规则)
     */
    @ApiOperation(value = "删除本体", notes = "删除本体")
    @ApiImplicitParam(name = "soid", value = "本体soid")
    @DeleteMapping(value = "/deleteKnwNoumenon")
    @ResponseBody
    public ActionResult delete(@RequestParam long soid) {
        return knwNoumenonService.delete(soid);
    }

    /**
     * 获取本体
     */
    @ApiOperation(value = "获取本体", notes = "获取本体")
    @ApiImplicitParam(name = "soid", value = "本体soid")
    @GetMapping(value = "/getKnwNoumenon")
    @ResponseBody
    public ListResult<com.mida.dms.knw.entity.vo.KnwNoumenonVo> get(@RequestParam Long soid) {
        return knwNoumenonService.get(soid);
    }

    /**
     *分页查询本体
     */
    @ApiOperation(value = "分页查询本体", notes = "分页查询本体")
    @PostMapping(value = "/findPage",consumes = { "application/json;charset=UTF-8" })
    @ResponseBody
    public ListResult<PageResultVo<com.mida.dms.knw.entity.vo.KnwNoumenonVo>> findPage(@RequestBody KnwNoumenonPageVo vo) {
        return knwNoumenonService.findPage(vo);
    }

    /**
     * 查重
     * @param originalSoid
     * @param newNoumenonType
     * @param newNoumenonSubType
     * @param newNoumenonShowName
     * @return
     */
    @ApiOperation(value = "重复性验证",notes = "重复性验证")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "originalSoid", value = "原本体soid"),
            @ApiImplicitParam(name = "newNoumenonType", value = "新本体分类代码"),
            @ApiImplicitParam(name = "newNoumenonSubType", value = "新本体领域类型"),
            @ApiImplicitParam(name = "newNoumenonShowName", value = "新本体显示名称")
    })
    @GetMapping(value = "/checkDataRepeat",produces = { "application/json;charset=UTF-8" })
    @ResponseBody
    public ActionResult checkDataRepeat(@RequestParam Long originalSoid,@RequestParam Long newNoumenonType,
                                        @RequestParam Long newNoumenonSubType,@RequestParam String newNoumenonShowName){
        return knwNoumenonService.checkName(originalSoid,newNoumenonType,newNoumenonSubType,newNoumenonShowName);
    }

    /**
     * 查询所有本体
     */
    @ApiOperation(value = "查询所有本体", notes = "查询所有本体")
    @GetMapping(value = "/findAll")
    @ResponseBody
    public ListResult<List<com.mida.dms.knw.entity.vo.KnwNoumenonVo>> findAll(@RequestParam String search) {
        return knwNoumenonService.findAll(search);
    }

    /**
     * 获取本体类型树状结构
     * @return
     */
    @ApiOperation(value = "获取本体类型树状结构",notes = "获取本体类型树状结构")
    @GetMapping(value = "/getTree")
    @ResponseBody
    public ListResult<List<KnwNoumenonFirstLevelTree>> getTree(){
        return knwNoumenonService.getTree();
    }

    /**
     * 查询本体树状结构子树
     * @return
     */
    @ApiOperation(value = "获取本体类型树状结构",notes = "获取本体类型树状结构")
    @GetMapping(value = "/getSubTree")
    @ResponseBody
    public ListResult<List<PullDownMenuVO>> getSubTree(@RequestParam("noumenonSoid") Long noumenonSoid){
        return knwNoumenonService.getSubTree(noumenonSoid);
    }

    /**
     * 10. 查询本体树(局部解剖学)根树
     * @param noumenonSoid
     * @return
     */
    @ApiOperation(value = "获取本体树(局部解剖学)根树",notes = "获取本体树(局部解剖学)根树")
    @GetMapping(value = "/getLocalAnatomyRootTree")
    @ResponseBody
    public ListResult<List<PullDownMenuVO>> getLocalAnatomyRootTree(@RequestParam("noumenonSoid") Long noumenonSoid){
        return knwNoumenonService.getLocalAnatomyRootTree(noumenonSoid);
    }

    /**
     * 11. 批量插入本体与授权空间
     * @param
     * @return
     */
    @PostMapping(value = "/bulkInsertknwNoumenonUserSpace")
    @ResponseBody
    public ActionResult bulkInsertknwNoumenonUserSpace(@RequestBody KnwNoumenonUserSpaceBatchVo knwNoumenonUserSpaceBatchVo ){
        ActionResult result = new ActionResult();
        try {
            return knwNoumenonService.bulkInsertknwNoumenonUserSpace(knwNoumenonUserSpaceBatchVo);
        } catch (Exception e) {
            log.error("批量插入本体与授权空间失败", e);
            buildActionWarn(result, "批量插入本体与授权空间失败");
        }
        return result;
    }


    /**
     * 12. 查询本体列表
     * @param
     * @return
     */
    @PostMapping(value = "/selectKnwNoumenonVos")
    @ResponseBody
    public ListResult<PageResultVo<com.mida.dms.knw.entity.vo.KnwNoumenonVo>> selectKnwNoumenonVos(@RequestBody KnwNoumenonPageVo vo){
        ListResult listResult = new ListResult();
        try {
            return knwNoumenonService.selectKnwNoumenonVos(vo);
        } catch (Exception e) {
            buildListWarn(listResult, "查询本体列表失败");
        }
        return listResult;
    }

}
